package com.hmall.trade.listener;

import com.hmall.api.client.PayClient;
import com.hmall.api.dto.PayOrderDTO;
import com.hmall.trade.constants.MQConstans;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class OrderDelayMessageListener {
private final IOrderService orderService;
private final PayClient payClient;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstans.DELAY_ORDER_QUEUE_NAME),
            exchange =@Exchange(name = MQConstans.DELAY_EXCHANGE_NAME,delayed = "true"),
            key=MQConstans.DELAY_ORDER_KEY
            ))
    public void listen(long orderId) {
        Order order = orderService.getById(orderId);
        if (order == null||order.getStatus()!=1) {
            return;
        }

        //查询支付流水
        PayOrderDTO payOrder = payClient.queryPayOrderByBizOrderNo(orderId);
        if (payOrder!=null&&payOrder.getStatus()==3){
            //已支付
            orderService.markOrderPaySuccess(orderId);
        }else {
            orderService.cancelOrder(order);
        }

    }
}
